package com.mzy.javaapi.IO.fileio;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/6/21 19:15
 * @Version 1.0
 */
@Slf4j
public class PropertiesReadWrite {
    /**
     * 读取文件内容
     *
     * @param path
     * @return
     */
    public static Properties readFile(String path) {
        File file = new File(path);
        try {
            Properties properties = new Properties();
            InputStream is = new FileInputStream(file);
            properties.load(is);
            return properties;
        } catch (IOException e) {
            log.error("file read error", e);
            throw new RuntimeException("文件读取失败，IO异常");
        }
    }

    /**
     * 写入/修改文件内容文件内容
     *
     * @param msg
     * @param path
     */
    public static void writeContent(String msg, String path) {
        File file = new File(path);
        try {
            Properties properties = new Properties();
            properties.setProperty("CONF_VERSION", msg);
            FileWriter fw = new FileWriter(file);
            properties.store(fw, "save confVersion");
        } catch (IOException e) {
            log.error("file write error", e);
            throw new RuntimeException("文件写入失败，IO异常");
        }
    }
}
